//
//  ViewController.m
//  CarTown
//
//  Created by 熊浩 on 15/7/30.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "ViewController.h"
#import "RootTabViewController.h"

#import "PostUitls.h"
#import "CompanyFindPassViewController.h"
#import "City.h"
#import <MBProgressHUD.h>
#import "ResMode.h"
#import "AppDelegate.h"
#import "Puser.h"
@interface ViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIView *login_pal_view;
@property (weak, nonatomic) IBOutlet UIButton *login_btn;
@property (weak, nonatomic) IBOutlet UIButton *reg_btn;
@property (weak, nonatomic) IBOutlet UITextField *username;

@property (weak, nonatomic) IBOutlet UITextField *password;
@end




@implementation ViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
     self.navigationController.navigationBarHidden=YES;
    
}



- (IBAction)loginclick:(id)sender {

       [self login];
       
}
-(void)login{
    if ([self chekForm]) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"正在登录中";
        hud.mode = MBProgressHUDModeIndeterminate;
        
        hud.removeFromSuperViewOnHide = YES;
        
        NSMutableDictionary *params=[NSMutableDictionary dictionary];
        params[@"action"]=Loginparams;
        params[@"username"]=self.username.text;
        params[@"pass"]=self.password.text;
        [PostUitls Post:loginurl parameters:params success:^(id response) {
            
            NSLog(@"%@",response);
            
            NSDictionary *dict=response;
            ResMode *mode=[ResMode modeWithDict:dict];
            if ([mode.state integerValue]==400) {
                [hud hide:YES];
                self.password.text=@"";
                self.username.text=@"";
                [XHUserDeflaut setBool:YES forKey:LOGINSTATE];
                [XHUserDeflaut setInteger:[mode.power integerValue] forKey:POWER];
                
                Puser *user= [Puser userWithDict:mode.data WithPower:mode.power];
                
                if ([mode.power integerValue]<5) {
                    [APService setAlias:user.user_mobile callbackSelector:@selector(tagsAliasCallback:tags:alias:) object:nil];
                }else{
                    [APService setAlias:user.com_username callbackSelector:@selector(tagsAliasCallback:tags:alias:) object:nil];
                }
               
                [self pushToTab];
            }else{
                hud.labelText=mode.msg;
                hud.mode=MBProgressHUDModeText;
                [hud hide:YES afterDelay:1];
            }
            
        } failure:^(NSError *error) {
            NSLog(@"%@",error);
            hud.labelText=@"网络错误";
            [hud hide:YES afterDelay:1];
        }];
    }

}

-(void)tagsAliasCallback:(int)iResCode tags:(NSSet *)tags alias:(NSString *)alias { NSLog(@"rescode: %d, \ntags: %@, \nalias: %@\n", iResCode, tags , alias);}

-(void)pushToTab{
    
    
    RootTabViewController *tab=[[RootTabViewController alloc]init];
    [self.navigationController pushViewController:tab animated:YES];
}

- (IBAction)companyfind:(id)sender {
    CompanyFindPassViewController *company=[CompanyFindPassViewController companyfind];
    [self.navigationController pushViewController:company animated:YES];
    
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    if (textField!=self.password) {
        [self.password becomeFirstResponder];
    }else{
        [self.password endEditing:YES];
        [self login];
    }
    return YES;
}
-(BOOL)chekForm{
    
    [self.username endEditing:YES];
    [self.password endEditing:YES];
    if (self.username.text.length==0) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"账号不能为空";
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return false;
    }
    if (self.password.text.length==0) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"密码不能为空";
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return false;
    }
    return true;
}


- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleDefault;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.login_pal_view.layer.cornerRadius=5;
    self.username.delegate=self;
    self.password.delegate=self;
   
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.username endEditing:YES];
    [self.password endEditing:YES];
    
}
@end
